package com.md.livingwidget;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.md.basedpc.PHBarUtils;
import com.md.basedpc.PHNoDoubleClickListener;
import com.md.basedpc.PHUtils;
import com.md.basedpc.base.BaseActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import androidx.appcompat.widget.Toolbar;

import static org.greenrobot.eventbus.ThreadMode.MAIN;

/**
 * Created by ryan
 *
 * @date 2019/12/17.
 * description：业务核心基类
 */
public abstract class CoreBaseActivity extends BaseActivity implements View.OnClickListener {
    private Toolbar mToolbar;
    private TextView toolBarTitle;
    private ImageView backBtn;
    private TextView closeBtn;
    private ImageView moreBtn;
    private ImageView moreBtnLeft;
    private TextView moreText;
    private LinearLayout navToolbarBarRoot;
    private View toolBarFengexian;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        initToolsBar();
        setStatusBarColor(false, getResources().getColor(R.color.white));
    }

    private void initToolsBar() {
        mToolbar = findViewById(R.id.nav_toolbar);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            mToolbar.setContentInsetsRelative(0, 0);
            toolBarTitle = findViewById(R.id.nav_toolbar_title);
            toolBarTitle.setText("");
            backBtn = findViewById(R.id.nav_toolbar_back_img);
            closeBtn = findViewById(R.id.nav_toolbar_back_text_back);
            moreBtnLeft = findViewById(R.id.nav_toolbar_right2_btn);
            moreBtn = findViewById(R.id.nav_toolbar_right3_btn);
            moreText = findViewById(R.id.nav_toolbar_right1_tv);
            toolBarFengexian = findViewById(R.id.nav_toolbar_bar_diver);
            navToolbarBarRoot = findViewById(R.id.nav_toolbar_bar_root);
            showBackImg();
            hideFengexian();
            findViewById(R.id.nav_toolbar_back).setOnClickListener(new PHNoDoubleClickListener() {
                protected void onNoDoubleClick(View view) {
                    backImgClick("back");
                }
            });
            closeBtn.setOnClickListener(new PHNoDoubleClickListener() {
                protected void onNoDoubleClick(View view) {
                    backBtnClick("back");
                }
            });
        }
    }

    public void setToolbarTitle(String title) {
        setToolbarTitle(title, true);
    }

    public void setToolbarTitle(String t, boolean b) {
        if (toolBarTitle != null) {
            toolBarTitle.setText(t);
        }
    }

    public void setToolbarTitle(String title, int c) {
        setToolbarTitle(title, true, c);
    }

    public void setToolbarTitle(String t, boolean b, int c) {
        if (toolBarTitle != null) {
            toolBarTitle.setText(t);
        }
        toolBarTitle.setTextColor(c);
    }

    public void setToolBarBackgroundColor(int bg) {
        if (navToolbarBarRoot != null)
            navToolbarBarRoot.setBackgroundColor(getmContext().getResources().getColor(bg));
    }

    public void setMoreBtnOnClick(PHNoDoubleClickListener listener) {
        if (moreBtn != null) {
            moreBtn.setOnClickListener(listener);
        }
    }

    public void setMoreTextOnClick(PHNoDoubleClickListener listener) {
        if (moreText != null) {
            moreText.setOnClickListener(listener);
        }
    }

    /**
     * 显示右侧第一个按钮
     */
    public void showMoreText() {
        if (moreText != null && moreText.getVisibility() != View.VISIBLE) {
            moreText.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置右侧第一个按钮显示文字
     *
     * @param str
     */
    public void setMoreText(String str) {
        if (moreText != null && moreText.getVisibility() != View.VISIBLE) {
            moreText.setText(str);
            moreText.setVisibility(View.VISIBLE);
        }
        if (moreText.getVisibility() == View.VISIBLE) {
            moreText.setText(str);
        }
    }

    public TextView getMoreText() {
        return moreText;
    }

    public void hideMoreText() {
        if (moreText != null && moreText.getVisibility() != View.GONE) {
            moreText.setVisibility(View.GONE);
        }
    }


    /**
     * 设置右侧第一个按钮的点击事件
     *
     * @param listener
     */
    public void setToolbarRightTextViewListener(PHNoDoubleClickListener listener) {
        if (null != moreText) {
            moreText.setOnClickListener(listener);
        }
    }

    /**
     * 设置右侧第一个按钮的文字颜色
     *
     * @param colorId
     */
    public void setToolbarRightTextViewColor(int colorId) {
        if (null != moreText) {
            moreText.setTextColor(getResources().getColor(colorId));
        }
    }

    public void setToolbarRightTextViewColor(int colorId, int type) {
        if (null != moreText) {
            moreText.setTextColor(colorId);
        }
    }

    /**
     * 显示分割线
     */
    public void showFengexian() {
        if (toolBarFengexian != null)
            toolBarFengexian.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏分割线
     */
    public void hideFengexian() {
        if (toolBarFengexian != null)
            toolBarFengexian.setVisibility(View.GONE);
    }

    public void hideToolbar() {
        if (mToolbar != null) {
            mToolbar.setVisibility(View.GONE);
        }
    }

    public void showToolbar() {
        if (mToolbar != null) {
            mToolbar.setVisibility(View.VISIBLE);
        }
    }

    public void backBtnClick(String tag) {
        if ("back".equals(tag)) {
            finish();
        }
    }

    public void backImgClick(String tag) {
        if ("back".equals(tag)) {
            finish();
        }
    }

    public void showBackImg() {
        if (backBtn != null) {
            backBtn.setTag("back");
            backBtn.setImageResource(R.drawable.ph_common_back);
            backBtn.setVisibility(View.VISIBLE);
        }
    }

    public void showBackImg(int src) {
        if (backBtn != null) {
            backBtn.setTag("back");
            backBtn.setImageResource(src);
            backBtn.setVisibility(View.VISIBLE);
        }
    }

    public void showBackImg(int src, String tag) {
        if (backBtn != null) {
            backBtn.setTag(tag);
            backBtn.setImageResource(src);
            backBtn.setVisibility(View.VISIBLE);
        }
    }

    public void hideBackImg() {
        if (backBtn != null) {
            backBtn.setVisibility(View.GONE);
        }
    }

    public void onNoDoubleClick(View v) {
    }

    public void showCloseBtn() {
        if (closeBtn != null) {
            // closeBtn.setBackground(getResources().getDrawable(R.drawable.ph_common_close));
            closeBtn.setVisibility(View.VISIBLE);
        }
    }

    public void hideCloseBtn() {
        if (closeBtn != null) {
            closeBtn.setVisibility(View.GONE);
        }
    }

    public void setCloseBtnText(String text) {
        if (closeBtn != null) {
            closeBtn.setText(text);
            closeBtn.setBackground(null);
            closeBtn.setBackgroundColor(getResources().getColor(R.color.transparent));
        }
    }

    public void setCloseBtnTextSize(float size) {
        if (closeBtn != null) {
            closeBtn.setTextSize(size);
        }
    }

    public void setCloseBtnOnClick(PHNoDoubleClickListener listener) {
        if (closeBtn != null) {
            closeBtn.setOnClickListener(listener);
        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = MAIN, sticky = true)
    public void onEvent(Object event) {
        onEventBase(event);
    }

    public void onEventBase(Object event) {
    }


}
